package com.ydy.service.impl;

import com.github.pagehelper.PageHelper;
import com.ydy.enums.CommentLevel;
import com.ydy.enums.YesOrNo;
import com.ydy.mapper.*;
import com.ydy.pojo.*;
import com.ydy.pojo.vo.CommentLevelCountsVO;
import com.ydy.pojo.vo.ItemCommentVO;
import com.ydy.pojo.vo.SearchItemsVO;
import com.ydy.pojo.vo.ShopCartVO;
import com.ydy.service.ItemService;
import com.ydy.utils.PageInfo;
import com.ydy.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ItemServiceImpl implements ItemService {

    public static final Logger log =
            LoggerFactory.getLogger(ItemServiceImpl.class);

    @Autowired
    private ItemsMapper itemsMapper;

    @Autowired
    private ItemsImgMapper itemsImgMapper;

    @Autowired
    private ItemsSpecMapper itemsSpecMapper;

    @Autowired
    private ItemsParamMapper itemsParamMapper;

    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;

    /**
     * 根据商品id查询商品信息
     * @Title: queryItemById
     * @author: dy.yin 2022/1/14 15:10
     * @param: [itemId]  商品id
     * @return: {@link Items }商品信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Items queryItemById(String itemId) {
        log.info("查询商品信息");
        return itemsMapper.selectByPrimaryKey(itemId);
    }

    /**
     * 根据商品id查询商品图片信息
     * @Title: queryItemsImgById
     * @author: dy.yin 2022/1/14 15:29
     * @param: [itemId]      商品id
     * @return: {@link List< ItemsImg> } 商品图片信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsImg> queryItemsImgById(String itemId) {
        ItemsImg itemsImg = new ItemsImg();
        itemsImg.setItemId(itemId);
        log.info("查询商品图片信息");
        return itemsImgMapper.select(itemsImg);
    }

    /**
     * 查询商品规格信息
     * @Title: queryItemsSpecById
     * @author: dy.yin 2022/1/14 15:38
     * @param: [itemId]   商品id
     * @return: {@link List< ItemsSpec> }商品规格信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsSpec> queryItemsSpecById(String itemId) {
        ItemsSpec  itemsSpec = new ItemsSpec();
        itemsSpec.setItemId(itemId);
        log.info("查询商品规格信息");
        return itemsSpecMapper.select(itemsSpec);
    }

    /**
     *  查询商品参数信息
     * @Title: queryItemsParamById
     * @author: dy.yin 2022/1/14 15:38
     * @param: [itemId]   商品id
     * @return: {@link ItemsParam } 商品参数信息
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ItemsParam queryItemsParamById(String itemId) {
        ItemsParam  itemsParam = new ItemsParam();
        itemsParam.setItemId(itemId);
        log.info("查询商品参数信息");
        return itemsParamMapper.selectOne(itemsParam);
    }

    /**
     * 根据商品id查询商品的评价等级数量
     * @Title: queryCommentCounts
     * @author: dy.yin 2022/1/14 17:00
     * @param: [itemId]  商品id
     * @return: {@link CommentLevelCountsVO } 评价等级数量
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public CommentLevelCountsVO queryCommentCounts(String itemId) {
        Integer goodCounts = getCommentCounts(itemId, CommentLevel.GOOD.type);
        Integer normalCounts = getCommentCounts(itemId, CommentLevel.NORMAL.type);
        Integer badCounts = getCommentCounts(itemId, CommentLevel.BAD.type);
        Integer totalCounts = goodCounts + normalCounts + badCounts;

        CommentLevelCountsVO countsVO = new CommentLevelCountsVO();
        countsVO.setTotalCounts(totalCounts);
        countsVO.setGoodCounts(goodCounts);
        countsVO.setNormalCounts(normalCounts);
        countsVO.setBadCounts(badCounts);

        return countsVO;
    }

    /**
     * 查询评价登记数量
     * @param itemId 商品id
     * @param level 评价等级
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    Integer getCommentCounts(String itemId, Integer level) {
        ItemsComments condition = new ItemsComments();
        condition.setItemId(itemId);
        if (level != null) {
            condition.setCommentLevel(level);
        }
        log.info("查询商品的评价等级数量");
        return itemsCommentsMapper.selectCount(condition);
    }

    /**
     * 根据商品id查询商品的评价（分页）
     * @Title: queryPagedComments
     * @author: dy.yin 2022/1/14 17:27
     * @param: itemId 商品id
     * @param: level 评价级别
     * @param: pageInfo 分页对象
     * @return: {@link PageInfo }
     */
    @Override
    public List<ItemCommentVO> queryPagedComments(String itemId, Integer level, PageInfo pageInfo) {
        PageUtil.StartPage(pageInfo);
        log.info("根据商品id:{}查询商品的评价",itemId);
        return itemsCommentsMapper.queryPagedComments(itemId,level);
    }

    /**
     * 通过关键字搜索商品列表
     * @param keywords 关键字
     * @param sort 排序方式
     * @param pageInfo 分页对象
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SearchItemsVO> searchItemsByKeyWords(String keywords, String sort,PageInfo pageInfo) {

        Map<String, Object> map = new HashMap<>();
        map.put("keywords", keywords);
        map.put("sort", sort);

        PageUtil.StartPage(pageInfo);
        List<SearchItemsVO> list = itemsMapper.searchItemsByKeyWords(map);
        log.info("根据关键字keyword:{}查询商品的列表",keywords);
        return list;
    }

    /**
     * 通过分类id搜索商品列表
     * @param catId 分类id
     * @param sort 排序规则
     * @param pageInfo 分页对象
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<SearchItemsVO> searchItemsByCatId(Integer catId, String sort,PageInfo pageInfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("catId", catId);
        map.put("sort", sort);

        log.info("根据分类id:{}查询商品的列表",catId);
        PageUtil.StartPage(pageInfo);
        return itemsMapper.searchItemsByCatId(map);
    }

    /**
     * 根据商品规格ids查找最新的商品数据
     * @param specIds 规格拼接ids
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ShopCartVO> queryItemsBySpecIds(String specIds) {
        List<String> specIdsList = Arrays.asList(specIds.split(","));
        log.info("根据商品规格ids:{}查找最新的商品数据",specIds);
        return itemsMapper.queryItemsBySpecIds(specIdsList);
    }

    /**
     * 查询商品规格
     * @param specId
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ItemsSpec queryItemSpecById(String specId) {
        return itemsSpecMapper.selectByPrimaryKey(specId);
    }

    /**
     * 根据商品id获得商品图片主图url
     * @param itemId 商品id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public String queryItemMainImgById(String itemId) {
        ItemsImg itemsImg = new ItemsImg();
        itemsImg.setItemId(itemId);
        itemsImg.setIsMain(YesOrNo.YES.type);
        ItemsImg result =  itemsImgMapper.selectOne(itemsImg);
        return result == null ? "" : result.getUrl();
    }

    /**
     * 减少库存
     * @param specId 规格id
     * @param buyCounts 购买数量
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void decreaseItemSpecStock(String specId, int buyCounts) {
        int result = itemsMapper.decreaseItemSpecStock(specId, buyCounts);
        if (result != 1) {
            throw new RuntimeException("订单创建失败，原因：库存不足!");
        }
    }
}
